﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using JXT.PrimaryKey.Batman.Core.Infrastructure;

namespace JXT.PrimaryKey.Batman.Web.Mvc.Results
{
    public class VerifyCodeResult : ViewResult
    {
        public string Code { get; set; }

        private VerifyCode _verifyCode = new VerifyCode();

        public VerifyCodeResult()
        {
            Code = _verifyCode.CreateVerifyCode(6);
            _verifyCode.FontSize = 14;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            //base.ExecuteResult(context);
            var httpResponse = context.HttpContext.Response;
            using (var image = _verifyCode.CreateImageCode(Code))
            {
                using (var ms = new System.IO.MemoryStream())
                {
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    httpResponse.ClearContent();
                    httpResponse.ContentType = "image/jpeg";
                    httpResponse.BinaryWrite(ms.GetBuffer());
                }
            }
        }
    }
}
